package com.theobserver.theobserver;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by wssxk on 7/7/17.
 */

public class NewsListAdapter extends BaseAdapter {
    Context mContext = null;
    public ArrayList<News> data = null;
    public NewsListAdapter(Context context)
    {
        mContext = context;
        data = new ArrayList<>();
    }

    public int getCount()
    {
        //return 20;
        return data.size();
    }

    public Object getItem(int pos)
    {
        return data.get(pos);
    }

    public long getItemId(int pos)
    {
        return pos;
    }

    public View getView(int pos, View convert, ViewGroup root) {
        View v = View.inflate(mContext, R.layout.news_item, null);
        /*
        if (convert == null) {
            v = View.inflate(mContext, R.layout.news_item, null);
        }
        else {
            v = convert;
        }
        */





        TextView news_title = (TextView) v.findViewById(R.id.textview_newsitem_title);
        TextView news_reporter = (TextView) v.findViewById(R.id.textview_newsitem_repoter);
        TextView news_date = (TextView) v.findViewById(R.id.textview_newsitem_date);
        TextView news_catalog = (TextView) v.findViewById(R.id.textview_newsitem_catalog);
        ImageView news_cover = (ImageView) v.findViewById(R.id.imageview_newsitem_cover);


        //Log.d("NEWS", data.get(pos).cur_news.getString("news_title"));
        //Toast.makeText(mContext, data.get(pos).cur_news.getString("news_title"), Toast.LENGTH_SHORT).show();

        news_title.setText(data.get(pos).cur_news.getString("news_title"));
        //news_reporter.setText(data.get(pos).reporter_name);
        news_reporter.setText(data.get(pos).cur_news.getAVObject("news_reporter").getString("user_name"));
        news_date.setText(data.get(pos).cur_news.getString("news_date"));
        news_catalog.setText(data.get(pos).cur_news.getAVObject("news_catalog").getString("catalog_name"));

        if(data.get(pos).news_cover != null) {
            news_cover.setImageBitmap(data.get(pos).news_cover);
        }
        else
        {
            news_cover.setImageResource(R.drawable.haha);
        }

        NewsItemFrameLayout news_framelayout = (NewsItemFrameLayout) v.findViewById(R.id.framelayout_news_item);
        news_framelayout.news = data.get(pos);

        return v;
    }


    public void clearData()
    {
        data.clear();
    }


    public void addItem(News n)
    {
        data.add(n);
    }

    public void addItems(ArrayList<News> ns)
    {
        data.addAll(ns);
    }
}
